<template>
  <div>
    <div style="border: 1px solid #ccc; margin-top: 10px">
      <!-- 工具栏 -->
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :defaultConfig="toolbarConfig"
      />
      <!-- 编辑器 -->
      <Editor
        style="height: 400px; overflow-y: hidden"
        :defaultConfig="editorConfig"
        v-model="html"
        @onChange="onChange"
        @onCreated="onCreated"
      />
    </div>
  </div>
</template>

<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

export default {
  name: "WangEditor",
  components: { Editor, Toolbar },
  props:{
    option:{
      type:Object,
      default(){
        return{}
      }
    },
    value:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      temp:'',
      editor: null,
      html: "<p>hello&nbsp;world</p>",
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单，如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
      },
      editorConfig: {
        placeholder: "请输入内容...",
        // autoFocus: false,

        // 所有的菜单配置，都要在 MENU_CONF 属性下
        MENU_CONF: {
          uploadImage: {
            server: process.env.VUE_APP_BASE_API + "/common/wangupload", // 上传图片的服务器地址
            fieldName: 'file', // 文件字段名
            maxFileSize: 2 * 1024 * 1024, // 图片大小限制，单位是字节
            maxNumberOfFiles: 5, // 一次最多上传多少张图片
            allowedFileTypes: ['image/jpeg', 'image/png', 'image/gif'], // 允许的图片格式
            headers: {
              Authorization: 'Bearer your_token_here', // 自定义请求头，例如认证 token
            },
            onBeforeUpload: (file) => {
              return file; // 可以在上传之前做些处理，返回 false 可以阻止上传
            },
            onSuccess: (file, res) => {
              console.log('图片上传成功', res);
            },
            onFailed: (file, res) => {
              console.error('图片上传失败', res);
            },
            onError: (file, err) => {
              console.error('图片上传出错', err);
            },
          },
        },
      },
    };
  },
  watch:{
    option:{
      handler(n){
        Object.entries(n).forEach(([k,v])=>{
          this.config[k]=v
        })
      },
      immediate:true
    },
    value:{
      handler(n){
        if(this.editor&&n!==this.temp){
          this.editor.setHtml(n)
        }
      },
      immediate:true
    }
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错
    },
    onChange(editor) {
      console.log("onChange", editor.getHtml()); // onChange 时获取编辑器最新内容
      this.$emit("input", editor.getHtml());
    },
    getEditorText() {
      const editor = this.editor;
      if (editor == null) return;
      console.log(editor.getText()); // 执行 editor API

    },
    printEditorHtml() {
      const editor = this.editor;
      if (editor == null) return;
      console.log(editor.getHtml()); // 执行 editor API

    },
  },

  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁 editor ，重要！！！
  },
};
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
